package com.xbongbong.paas.pojo.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.pojo.ExcelPartialExportPojo;
import com.xbongbong.paas.pojo.ShareConditionFormatPojo;
import com.xbongbong.paas.pojo.vo.DistributorMapVO;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.QueryIgnore;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.es.pojo.EsScriptConditionPojo;
import com.xbongbong.pro.statistic.pojo.FiscalYearTimePojo;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.domain.entity.UserEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author: wufeng
 * @date: 2018/9/4 19:51
 * @desrcption: 数据列表dto
 */
@Getter
@Setter
@ToString
public class FormDataListDTO extends BaseDTO {

    private static final long serialVersionUID = 5704113611252994668L;

    /**
     * 应用id
     */
    @NotNull(message = "{dto.msg.formDataList.appId.NotNull}")
    @Getter(onMethod_ = {@QueryIgnore})
    private Long appId;

    /**
     * 菜单id
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long menuId;

    /**
     * 分页当前页
     */
    @Getter(onMethod_ = {@QueryIgnore})
    @Min(value=1, message = "{dto.msg.formDataList.page.Min}")
    private Integer page;

    /**
     * 分页每页记录数
     */
    @Getter(onMethod_ = {@QueryIgnore})
    @Min(value=1, message = "{dto.msg.formDataList.page.Min}")
    private Integer pageSize;

    /**
     * 搜索条件
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<ConditionsEntityExt> conditions = new ArrayList<>();

    /**
     * 共享权限条件
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private ShareConditionFormatPojo sharePermission;

    /**
     *  or：任一； and：全部（筛选项的条件是任一一个满足，还是需要全部满足）
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private String rel;

    /**
     * 显示的字段
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<String> fieldList;

    /**
     * 排序字段{"field":"updateTime","sort":"acs"}
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Map<String,String> sortMap;

    private Integer del;

    /**
     * 分组id
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long listGroupId;

    /**
     * 是否默认分组 2 公海分组 1 系统分组 0是自定义分组
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer defaultGroup;
    /**
     * saas标记
     */
    @Getter(onMethod_ = {@QueryIgnore})
    @NotNull(message = "{dto.msg.formDataList.saasMark.NotNull}")
    @Max(value = 2)
    @Min(value = 1)
    private Integer saasMark;
    /**
     * saas业务类型
     */
    @Getter(onMethod_ = {@QueryIgnore})
    @NotNull(message = "{dto.msg.formDataList.businessType.NotNull}")
    private Integer businessType;
    /**
     * saas子业务类型
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer subBusinessType;
    /**
     * 表单id
     */
    private Long formId;

    @Getter(onMethod_ = {@QueryIgnore})
    private Map<String, FieldAttrEntity> explainMap;

    /**
     * 前端模糊搜索参数
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private String nameLike;

    /**
     * 前端模糊搜索参数,产品特有
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Long> nameLikeIdIn;

    /**
     * 是否简单列表查询
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Boolean simple;

    /**
     * 审批状态
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Integer> flowStatusIn;

    /**
     *  查询产品id
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Long> productSearchIds;

    /**
     * saas列表数据状态
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer statusFilter;

    /**
     * 特殊列表筛选
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private JSONObject commonFilter;

    /**
     * es脚本pojo
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private EsScriptConditionPojo esScriptPojo;
    /**
     * es脚本source内的语句
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private String esScriptSourceStr;

    /**
     * 移动端列表特殊筛选
     * mobileSpecialFilter:[{
     *      "param":[
     *          "1",
     *          "2"
     *      ],
     *      "saasMobileFilterType":0
     * }]
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private JSONArray mobileSpecialFilter;

    @Getter(onMethod_ = {@QueryIgnore})
    private List<List<ConditionsEntityExt>> shouldConditions = new ArrayList<>();

    /**
     * 库存查询有大量子文档需求，抽离
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<ConditionsEntityExt> productSerchConditions = new ArrayList<>();
    /**
     * 导出为1
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer fromExport;
    /**
     * 部分导出
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private ExcelPartialExportPojo excelPartialExportPojo;
    /**
     * 勾选导出
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Long> idIn;

    /**
     * 勾选导出,产品参数使用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<String> linkIdIn;

    /**
     * 产品展开时的父产品id
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long parentId;

    /**
     * 导出使用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Map<String, Object> userMap;

    @Getter(onMethod_ = {@QueryIgnore})
    private Map<String, Object> deptMap;

    @Getter(onMethod_ = {@QueryIgnore})
    private Map<String, UserEntity> userAllMap;

    /**
     * 金额字段千分位解析是否需要保留原值
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Boolean analysisAmountOfThousandBitsWithOriginalValue;

    /**
     * 库存查询导出 子父产品对应map
     */
    private Map<Long, JSONArray> childProductMap;

    /**
     * 库存查询导出 父产品对应的仓库名称map
     */
    private Map<Long, String> productWarehouseMap;

    /**
     * 库存查询导出
     */
    private Map<Long, Set<String>> productWarehousesMap;

    /**
     * 库存查询导出 子产品对应的仓库库存
     */
    private Map<String, Double> productStockMap;

    /**
     * 多表单限制
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Boolean limitForms = false;

    /**
     * 多表单
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Long> formIdList;
    /**
     * 关联属性相关字段
     */
    @Valid
    @Getter(onMethod_ = {@QueryIgnore})
    private LinkInfoPojo linkInfo;
    /**
     * 筛选的仓库id 库存查询下拉仓库筛选
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Long> selectWarehouseIdList;
    /**
     * 预警查询用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Map<Long, PaasFormDataEntityExt> productEntityMap;
    /**
     * 库存预警用
     */
    private List<Long> includeIds;
    @Getter(onMethod_ = {@QueryIgnore})
    private Long updateTime;
    @Getter(onMethod_ = {@QueryIgnore})
    private Map<Long, PaasFormEntityExt> formMap;
    @Getter(onMethod_ = {@QueryIgnore})
    private Map<Long, List<FieldAttrEntity>> formIdAndHeadListMap;

    @Getter(onMethod_ = {@QueryIgnore})
    private FiscalYearTimePojo timeFilter;
    /**
     * 是否需要查询子表单
     * true：需要；false：不需要
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Boolean searchSubForm;

    /**
     * 子表单显示的字段
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<String> subFieldList;
    /**
     * 是否是首页自定义指标穿透(给穿透产品用)
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private boolean through;
    /**
     * 是否是穿透过来的
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Boolean fromThrough = false;
    /**
     * 经销商区域视图标识
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer isDistributorView;
    /**
     * 是否是经销商地图
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer isDistributorMap;
    /**
     * 前端返回的地图穿透对象
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private DistributorMapVO distributorMapVO;
    /**
     * 前端返回的地址筛选需要穿透到散点图
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<String> distributorAddress;

    /**
     * 标识为是否为客户-联系人
     */
    private Integer isContact;

    /**
     * 泳道视图版本id
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long stageProcessId;

    /**
     * 泳道视图阶段Code
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private String stageCode;

    /**
     * 是否查询老数据标识
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Boolean legacyDataFlag;

    /**
     * 自定义、系统标志
     * @see com.xbongbong.pro.statistic.enums.StatisticsTypeEnum
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer statisticsType;

    /**
     * 待入库产品信息
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<PurchaseProductEntity> purchaseProductEntityList;

    /**
     * 待出库产品信息
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<ContractProductEntity> contractProductEntityList;

    /**
     * 待采购合同视图一次查询出来的所有数据中最后一个更新时间
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long lastUpdateTime;

    /**
     * 待采购合同视图返回列表页所有数据中最后一个更新时间
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long lastTime;

    /**
     * 待采购合同视图第一个更新时间
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long firstTime;

    /**
     * 待采购合同视图上一页
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer onPage;

    /**
     * 待采购合同视图是否最后一页  1是 0不是
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer isLast;

    /**
     * 待采购合同视图es查询的长度
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer searchPageSize;



    /**
     * 知识库ID，知识库用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long knowledgeBaseId;

    /**
     * 目录ID，知识库用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long catalogueId;

    /**
     * 知识条目状态，知识库用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer status;

    /**
     * 标签ID，知识库用
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private List<Long> tags;

    /**
     * 导出使用 ，上次查询的最后一条数据，用于确认下次查询的起始位置
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Object[] searchAfterList;

    /**
     * 工单池id
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Long poolId;


    /**
     * 是否地图视图
     */
    @Getter(onMethod_ = {@QueryIgnore})
    private Integer isMapView;

    public FormDataListDTO() {
        rel = "and";
        page = 1;
        pageSize = 20;
        del = 0;
        simple = false;
        sortMap = new HashMap<>();
    }

    public FormDataListDTO(Integer saasMark, Integer businessType, Long formId) {
        this.saasMark = saasMark;
        this.businessType = businessType;
        this.formId = formId;
    }

    public void setDefaultGroup(Integer defaultGroup) {
        if (defaultGroup != null){
            this.defaultGroup = defaultGroup;
        }
    }

    public void setSubBusinessType(Integer subBusinessType) {
        this.subBusinessType = subBusinessType;
        if (Objects.equals(XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode(),subBusinessType)){
            this.defaultGroup = 2;
        }
    }


    public FormDataListDTO(String corpid, Long formId, Integer saasMark, Integer businessType, Integer subBusinessType, Integer distributorMark, UserVO userVO, Boolean analysisAmountOfThousandBitsWithOriginalValue) {
        setCorpid(corpid);
        setDistributorMark(distributorMark);
        this.formId = formId;
        this.saasMark = saasMark;
        this.businessType = businessType;
        this.subBusinessType = subBusinessType;
        this.analysisAmountOfThousandBitsWithOriginalValue = analysisAmountOfThousandBitsWithOriginalValue;
        setLoginUser(userVO);
        setUserId(userVO.getUserId());
        setLoginUserName(userVO.getName());
    }


    public FormDataListDTO(String corpid, Long formId, Integer saasMark, Integer businessType, Integer subBusinessType, Integer distributorMark, UserVO userVO, Boolean analysisAmountOfThousandBitsWithOriginalValue, Integer page, Integer pageSize) {
        setCorpid(corpid);
        setDistributorMark(distributorMark);
        this.formId = formId;
        this.saasMark = saasMark;
        this.businessType = businessType;
        this.subBusinessType = subBusinessType;
        this.analysisAmountOfThousandBitsWithOriginalValue = analysisAmountOfThousandBitsWithOriginalValue;
        this.page = page;
        this.pageSize = pageSize;
        setLoginUser(userVO);
        setUserId(userVO.getUserId());
        setLoginUserName(userVO.getName());
    }

    /**
     * 工单2.0构造方法
     * @param corpid
     * @param userVO
     * @param listGroupId
     * @param defaultGroup
     * @param businessType
     * @param subBusinessType
     * @param formId
     * @param poolId
     */
    public FormDataListDTO(String corpid, UserVO userVO, Long listGroupId, Integer defaultGroup,  Integer businessType, Integer subBusinessType, Long formId, Long poolId) {
        setCorpid(corpid);
        setLoginUser(userVO);
        this.listGroupId = listGroupId;
        this.defaultGroup = defaultGroup;
        this.businessType = businessType;
        this.subBusinessType = subBusinessType;
        this.formId = formId;
        this.poolId = poolId;
    }
}
